﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Runtime.Remoting;
using System.Web.Http;
using SalesSys.BLL;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Models;
using SalesSys.Service.Sales;
using SalesSys.WebCore;
using SalesSys.WebCore.Authorize;

namespace SalesSys.Site.Controllers.Api
{
    /// <summary>
    /// 上传文件
    /// </summary>
    [RoutePrefix("api/Upload")]
    public class UploadController : BaseApiController
    {
        /// <summary>
        /// 上传收货凭证
        /// </summary>
        /// <param name="orderId">计划单编号</param>
        /// <returns></returns>
        [Route("OrderVoucher")]
        public bool OrderVoucher(Guid orderId)
        {
            OrderVoucherBLL voucherBll = BllFactory.GetBll<OrderVoucherBLL>();
            MultipartFormDataStreamProvider sp = this.GetFiles();
            List<OrderVoucher> vouchers = new List<OrderVoucher>();
            List<string> names = new List<string>();
            foreach (MultipartFileData file in sp.FileData)
            {
                vouchers.Add(new OrderVoucher() { OrderId = orderId, FileName = Path.GetFileName(file.LocalFileName) });
                names.Add(Path.GetFileName(file.LocalFileName));
            }
            // voucherBll.Create(vouchers);
            IDirectSale saleSvc = new SalesSvc();
            saleSvc.SetReceipt(orderId, names.ToArray());
            return true;

        }
        /// <summary>
        /// 客户信息上传,当前上传路径：file/customer
        /// </summary>
        /// <param name="customerId">客户编号</param>
        /// <returns></returns>
        [Route("CustomerInfo")]
        public bool CustomerInfo(Guid customerId)
        {
            var bll = BllFactory.GetBll<BaseBLL<FileList>>();
            MultipartFormDataStreamProvider sp = this.GetFiles("file/customer");
            var files = new List<FileList>();
            List<string> names = new List<string>();

            foreach (MultipartFileData file in sp.FileData)
            {
                files.Add(new FileList() { FromType = FileList.CustomerInfo, LinkId = customerId, FileName = Path.GetFileName(file.LocalFileName), FileExtName = Path.GetExtension(file.LocalFileName), CreaterId = ClientHelper.ClientInfo.Id });
                names.Add(Path.GetFileName(file.LocalFileName));
            }
            bll.Create(files);
            return true;
        }

        /// <summary>
        /// 获取贸易商凭证
        /// </summary>
        /// <param name="id">贸易商编号</param>
        /// <returns></returns>
        [Route("MerchantInfo")]
        public bool MerchantInfo(Guid id)
        {
            var path = "file/merchant";
            var type = 2;
            return AddFile(id, path, type);
        }

        /// <summary>
        /// 获取贸易商凭证
        /// </summary>
        /// <param name="id">贸易商编号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMerchantInfo")]
        public IResultState<FileList[]> GetMerchantInfo(Guid id)
        {
            var type = 2;
            return ReState.Success.CreateResult("", getFiles(type, id));
        }

        /// <summary>
        /// 获取客户报备图片信息
        /// </summary>
        /// <param name="customerId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetCustomerInfo")]
        public IResultState<FileList[]> GetCustomerInfo(Guid customerId)
        {
            var bll = BllFactory.GetBll<BaseBLL<FileList>>();
            var re = bll.GetList(p => p.FromType == FileList.CustomerInfo && p.LinkId == customerId);
            return ReState.Success.CreateResult("", re);
        }

        #region private

        /// <summary>
        /// 删除指定id列表文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        bool DeleteFiles(Guid[] ids)
        {
            var bll = BllFactory.GetBll<BaseBLL<FileList, Guid>>();
            return bll.Delete(ids) > 0;
        }
        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="path">路径</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        private bool AddFile(Guid id, string path, int type)
        {
            var bll = BllFactory.GetBll<BaseBLL<FileList>>();
            MultipartFormDataStreamProvider sp = this.GetFiles(path);
            var files = new List<FileList>();
            List<string> names = new List<string>();

            foreach (MultipartFileData file in sp.FileData)
            {
                files.Add(new FileList()
                {
                    FromType = type,
                    LinkId = id,
                    FileName = Path.GetFileName(file.LocalFileName),
                    FileExtName = Path.GetExtension(file.LocalFileName),
                    CreaterId = ClientHelper.ClientInfo.Id
                });
                names.Add(Path.GetFileName(file.LocalFileName));
            }
            bll.Create(files);
            return true;
        }

        private FileList[] getFiles(int type, Guid id)
        {

            var bll = BllFactory.GetBll<BaseBLL<FileList>>();
            var re = bll.GetList(p => p.FromType == type && p.LinkId == id);
            return re;
        }

        #endregion

    }
}